<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="单价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入单价"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="过期时间" prop="expireTime">
        <el-input
          v-model="queryParams.expireTime"
          placeholder="请输入过期时间"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="题库ID" align="center" prop="bankId" v-if="false"/>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="主图" align="center" prop="mainImg" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="过期时间" align="center" prop="expireTime" />
      <el-table-column label="状态" align="center" prop="status" v-if="false">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
        v-if="false" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('bank', scope.row.id, scope.row.name, scope.row.price)"
            v-hasPermi="['exam:bank-spu:update']"
          >
            选购
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：选购 -->
  <ExamOrderForm ref="formRef" @success="orderPay" />
</template>

<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { BankSpuApi, BankSpuVO } from '@/api/exam/bankspu'
import ExamOrderForm from "@/views/exam/center/member/spu/components/ExamOrderForm.vue";
const { push } = useRouter() // 路由跳转
/** 题库-SPU 列表 */
defineOptions({ name: 'MemberBankSpu' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<BankSpuVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  bankId: undefined,
  name: undefined,
  price: undefined,
  expireTime: undefined,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BankSpuApi.getBankSpuPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (spuType: string, spuId?: number, spuName?: String, price?: number) => {
  formRef.value.open(spuType, spuId, spuName, price)
}
const orderPay = () => {
  push({ name: 'MemberExamOrder' })
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
